package com.my.recyclerandrecycler;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.my.recyclerandrecycler.adapter.MainAdapter;
import com.my.recyclerandrecycler.bean.ShoppingBean;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private Button btnJs;
    private CheckBox cbAll;
    private RecyclerView recyclerView;
    private TextView tvPrice;
    private ShoppingBean shopCartBeans;
    private List<ShoppingBean.DataBean> data;
    private GridLayoutManager gridLayoutManager;
    private MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initListener();
    }

    private void initView() {
        btnJs = findViewById(R.id.btn_js);
        cbAll = findViewById(R.id.cb_select_all);
        recyclerView = findViewById(R.id.rv_main);
        tvPrice = findViewById(R.id.tv_price);
        gridLayoutManager = new GridLayoutManager(this, 1, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);
        initData();
    }

    private void initListener() {
        btnJs.setOnClickListener(this);
        cbAll.setOnCheckedChangeListener(this);
    }

    private void initData() {
        String jsonData = "{\"status\":true,\"msg\":\"\",\"data\":[{\"store_name\":\"微超优品\",\"user_id\":\"229\",\"store_id\":\"14828331510902860000\",\"list\":[{\"goods_price\":183,\"cart_id\":\"15232812841628050000\",\"user_id\":\"229\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15162475454444720000\",\"goods_num\":\"1\",\"goods_name\":\"飞科FC5901专业发廊理发器电推剪电动推子成人老人儿童剃头\",\"goods_image\":\"http://img.lion-mall.com/goods/20180118/dbff92fc8dee4e26c39b21ee207a18a8.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"型号\",\"spec1_value\":\"FC5901(积分价)\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":66,\"is_have_point\":\"1\",\"model_id\":\"15162475454434460000\"}]},{\"store_name\":\"本港海产\",\"user_id\":\"3096\",\"store_id\":\"15132364355633290000\",\"list\":[{\"goods_price\":33,\"cart_id\":\"15232812763901580000\",\"user_id\":\"3096\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15156635008014120000\",\"goods_num\":\"1\",\"goods_name\":\"本港海产 即食大片海苔原味辣味125g包邮\",\"goods_image\":\"http://img.lion-mall.com/goods/20180111/65aae13eb49e93939850c707dbf69966.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"规格\",\"spec1_value\":\"1*125g(积分价)\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":7,\"is_have_point\":\"1\",\"model_id\":\"15156635008003640000\"}]},{\"store_name\":\"智能生活屋\",\"user_id\":\"3090\",\"store_id\":\"15110923292896270000\",\"list\":[{\"goods_price\":98,\"cart_id\":\"15232812214585490000\",\"user_id\":\"3090\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15154810744781680000\",\"goods_num\":\"1\",\"goods_name\":\"杜酷（DUKU） 无线蓝牙键盘多屏双通道蓝牙键盘通用\",\"goods_image\":\"http://img.lion-mall.com/goods/20180109/8885a44d743b75ccb0f3601686ddb719.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"颜色\",\"spec1_value\":\"金色(积分价)\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":31,\"is_have_point\":\"1\",\"model_id\":\"15154810744751190000\"}]},{\"store_name\":\"聚美佳品\",\"user_id\":\"2985\",\"store_id\":\"15028511939255260000\",\"list\":[{\"goods_price\":48,\"cart_id\":\"15232475994654440000\",\"user_id\":\"2985\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15153090695659930000\",\"goods_num\":\"2\",\"goods_name\":\"卡通抱枕被子两用 多功能暖手汽车空调被三合一\",\"goods_image\":\"http://img.lion-mall.com/goods/20180107/568058f3aaf01cd0297b26d75c1ff85a.png\",\"spec_desc\":\"\",\"spec1_name\":\"颜色\",\"spec1_value\":\"可爱猫\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":0,\"is_have_point\":\"1\",\"model_id\":\"15153090695654650000\"}]},{\"store_name\":\"羽森家纺\",\"user_id\":\"267\",\"store_id\":\"14830180850588560000\",\"list\":[{\"goods_price\":380,\"cart_id\":\"15232475900203700000\",\"user_id\":\"267\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15155019232807690000\",\"goods_num\":\"1\",\"goods_name\":\"羽森高档双层纱贡缎阳绒四件套\",\"goods_image\":\"http://img.lion-mall.com/goods/20180109/7de0db542473faf6659ed457dda87275.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"颜色\",\"spec1_value\":\"蒙特城堡（香槟灰）\",\"spec2_name\":\"规格\",\"spec2_value\":\"200*230\",\"proportion_return\":\"50\",\"goods_points\":0,\"is_have_point\":\"1\",\"model_id\":\"15155019232792440000\"}]},{\"store_name\":\"戈勒世家\",\"user_id\":\"1098\",\"store_id\":\"14879242960201500000\",\"list\":[{\"goods_price\":67,\"cart_id\":\"15232475545649120000\",\"user_id\":\"1098\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15168503821784490000\",\"goods_num\":\"2\",\"goods_name\":\"男士特色独眼怪兽胸包单肩包斜挎包时尚户外休闲包潮流男包\",\"goods_image\":\"http://img.lion-mall.com/goods/20180125/a355d78946c721565d2e44687f15f934.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"颜色\",\"spec1_value\":\"黑色(积分兑)\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":12,\"is_have_point\":\"1\",\"model_id\":\"15168503821773910000\"},{\"goods_price\":82,\"cart_id\":\"15232475436635450000\",\"user_id\":\"1098\",\"member_id\":\"15222940696559300000\",\"goods_id\":\"15168478480459580000\",\"goods_num\":\"1\",\"goods_name\":\"男士手提包AD18单肩包斜挎皮包商务公文包手提包\",\"goods_image\":\"http://img.lion-mall.com/goods/20180125/31966da605420f672712bd07a36dcd4a.jpg\",\"spec_desc\":\"\",\"spec1_name\":\"颜色\",\"spec1_value\":\"黑色(积分兑)\",\"spec2_name\":\"\",\"spec2_value\":\"\",\"proportion_return\":\"50\",\"goods_points\":23,\"is_have_point\":\"1\",\"model_id\":\"15168478480445350000\"}]}]}";
        Gson gson = new Gson();
        shopCartBeans = gson.fromJson(jsonData, ShoppingBean.class);
        data = null;
        data = shopCartBeans.getData();

        adapter = new MainAdapter(this, data);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_js) {
            Log.i("mwb", "btn_onClick");
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.cb_select_all) {
            if (isChecked) {
                adapter.setSelet(isChecked);
            } else {
                adapter.setSelet(isChecked);
            }
        }
    }

}
